// packageF/others/customerCenter/customerCenterIndex/customerCenterIndex.js
const app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
const DrainageScene = require('../../../../utils/drainage_scene.js')

Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    navInd: 0,
    searchKey: '',
    show: false,
    showIndex: '',
    datainfo: [],
    list: [],
    display_set: {},
    userLeave: [],
    // 筛选条件
    //是否推广员
    is_agent: '',
    //会员等级id
    level_id: {
      name: '',
      id: ''
    },
    //排序
    orderby: {
      name: '',
      id: ''
    },
    // 轨迹
    browse: {
      name: '',
      id: ''
    },

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.getData();
    this.getList();
    wx.setNavigationBarTitle({
      title: '客户中心',
    })
  },

  toPlugin(e){
    let name = e.currentTarget.dataset.name;
    if(name == 'material-center'){
      // materialCenter
      wx.navigateTo({
        url: '/packageA/others/materialCenter/materialCenter',
      })
    }else if(name == 'article'){
      // notice
      wx.navigateTo({
        url: '/packageA/member/notice/notice',
      })
    }else if(name == 'video-share'){
      wx.navigateTo({
        url: '/packageC/video_goods/VideoList/VideoList',
      })
    }else if(name == "micro-communities"){
      wx.navigateTo({
        url: '/packageC/micro_communities/microIndex/microIndex',
      })
    }
  },
  goTrack() {
    wx.navigateTo({
      url: '/packageF/others/customerCenter/customerCenterTrack/customerCenterTrack?is_type=' + this.data.navInd,
    })
  },
  telCall(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  onCopyWechat(e) {
    let wechat = e.currentTarget.dataset.wechat;
    wx.setClipboardData({
      data: wechat,
      success: () => {
        wx.hideToast()
        wx.showModal({
          content: '复制微信号成功',
          showCancel: false
        })
      }
    })
  },
  onConfirm(e) {
    let val = e.detail.value;
    if (this.data.showIndex == 0) {
      this.setData({
        'level_id.id': val.id,
        'level_id.name': val.level_name
      })
    } else if (this.data.showIndex == 1) {
      console.log(val,'dddddddddddd')
      this.setData({
        is_agent: val.id
      })
    } else if (this.data.showIndex == 2) {
      this.setData({
        orderby: val
      })
    } else {
      this.setData({
        browse: val
      })
    }
    this.setData({
      show: false
    })
    this.getList();
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  tapNav(e) {
    let ind = e.currentTarget.dataset.key;
    this.setData({
      navInd: ind
    })
    this.init();
    this.getList();
  },
  init() {
    // this.list = [];
    // this.data.isLoadMore = true;
    // this.data.page = 1;
    // this.data.total_page = 0;
    // // 初始化搜索条件
    // this.data.keyword = '';
    // this.data.is_agent = '';
    // this.data.level_id = {
    //   name: '',
    //   id: ''
    // };
    // this.data.orderby = {
    //   name: '注册时间倒序',
    //   id: '0'
    // };
    // this.data.browse = {
    //   name: '',
    //   id: ''
    // };
    this.setData({
      isLoadMore:true,
      page:1,
      total_page:0,
      keyword:'',
      is_agent:'',
      'level_id.name':'',
      'level_id.id':'',
      'orderby.name':'注册时间倒序',
      'orderby.id':0,
      'browse.name':'',
      'browse.id':''
    })
  },
  setShowInd(e) {
    console.log(e);
    let key = e.currentTarget.dataset.key;
    if (key == 0 && this.data.userLeave.length == 0) {
      this.getUser();
    }
    this.setData({
      showIndex: key,
      show: true
    })
  },
  onClickLeft() {
    this.setData({
      show: false
    })
  },
  onClickRight() {
    console.log(this.data.showIndex);
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.index.index');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let isBindDrainageScene = DrainageScene.bindDrainageScene({
            scene: 5,
            title: '客户中心'
          });
          if(!isBindDrainageScene) return;//爆客码绑定场景

          this.setData({
            datainfo: res.data,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getUser() {
    let urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.index.get-level-list');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let list = res.data.level_list;
          list = [{
            id: '',
            level_name:"请选择会员等级"
          }, ...list]
          console.log(list)
          this.setData({
            userLeave: list,
          })
          console.log(this.data.userLeave)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onCopy() {
    wx.setClipboardData({
      data: this.data.datainfo.member.invite_code,
      success(res) {}
    })
  },
  onChange(e) {
    console.log(e)
    this.setData({
      searchKey: e.detail
    })
  },
  getList() {
    let that = this;
    let json = {
      search: {
        level_type: Number(this.data.navInd),
        keyword: this.data.searchKey,
      }
    }
    // 推广员
    json.search.is_agent = this.data.is_agent;
    // 排序
    json.search.orderby = this.data.orderby.id;
    // 会员等级
    if (this.data.level_id && this.data.level_id.id) {
      json.search.level_id = this.data.level_id.id
    }
    // 轨迹
    if (this.data.browse && this.data.browse.id) {
      json.search.browse = this.data.browse.id
    }
    let urlStr = app.getNetAddresss("plugin.customer-center.frontend.controllers.index.get-customer-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.customer_list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            list: res.data.customer_list.data,
            display_set: res.data.display_set
          })
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let json = {
        search: {
          level_type: Number(this.data.navInd),
          keyword: this.data.searchKey,
        }
      }
      // 推广员
      json.search.is_agent = this.data.is_agent;
      // 排序
      json.search.orderby = this.data.orderby.id;
      // 会员等级
      if (this.data.level_id && this.data.level_id.id) {
        json.search.level_id = this.data.level_id.id
      }
      // 轨迹
      if (this.data.browse && this.data.browse.id) {
        json.search.browse = this.data.browse.id
      }
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.index.get-customer-list');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.customer_list.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})